from django.db import models
from django.contrib.auth.models import AbstractUser
from zhenghanshu2.utils.models import BaseModel


# Create your models here.
class User(AbstractUser):
    username = models.CharField(verbose_name="用户名", max_length=11, unique=True)
    mobile = models.CharField(verbose_name="手机号", max_length=11, unique=True)
    password = models.CharField(verbose_name="密码", max_length=200)
    youMoney = models.DecimalField(max_digits=16, decimal_places=2, null=True, blank=True, verbose_name="你拥有的钱", default=100000000.00)
    image = models.CharField(verbose_name="头像链接", max_length=200, null=True, blank=True)
    avater = models.ImageField(upload_to='avater', verbose_name='用户头像', null=True, blank=True,
                               help_text="头像大小规格：128*128")

    class Meta:
        db_table = 'taobao_users'
        verbose_name = '淘宝用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.username


class UserCart(BaseModel):
    product_much = models.IntegerField(verbose_name="购买的数量", default=1)
    product_image = models.CharField(max_length=500, verbose_name='商品的图片', null=True, blank=True, help_text="图片大小无要求")
    product_name = models.CharField(max_length=150, verbose_name='商品的名字')
    product_price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="商品的价格", default=0)
    introduce = models.TextField(max_length=2048, verbose_name="详情介绍", null=True, blank=True)
    user_name = models.ForeignKey("User", on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="购物车对应的用户")
    howManyToBuy = models.IntegerField(verbose_name="购买人数", default=0, null=True, blank=True)


    class Meta:
        db_table = 'taobao_userscart'
        verbose_name = '淘宝用户的购物车'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.product_name

    @property
    def usercart_mobile(self):
        return self.user_name.mobile


class UserCartDone(BaseModel):
    product_much = models.IntegerField(verbose_name="购买的数量", default=1)
    product_image = models.CharField(max_length=500, verbose_name='车的图片', null=True, blank=True, help_text="图片大小无要求")
    product_name = models.CharField(max_length=150, verbose_name='车的名字')
    product_price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="车的价格", default=0)
    introduce = models.TextField(max_length=2048, verbose_name="详情介绍", null=True, blank=True)
    user_name = models.ForeignKey("User", on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="购物车对应的用户")

    class Meta:
        db_table = 'taobao_userscartdone'
        verbose_name = '淘宝用户已购买的商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.product_name

    @property
    def usercart_mobile(self):
        return self.user_name.mobile


class UserPinglun(BaseModel):
    content = models.CharField(max_length=1500, verbose_name='用户评论')
    user_name = models.ForeignKey("User", on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="购物车对应的用户")
    product_name = models.ForeignKey("UserCart",on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="购物车对应的评论")
    class Meta:
        db_table = 'taobao_uerpinglun'
        verbose_name = '淘宝用户的评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.content

    @property
    def usercart_mobile(self):
        return self.user_name.mobile

    @property
    def usercart_usercart(self):
        return self.product_name.product_name
